//
//  ConmentCell.m
//  MiBaoTu
//
//  Created by mac004 on 2016/10/18.
//  Copyright © 2016年 com.caesartech15. All rights reserved.
//

#import "ConmentCell.h"

@implementation ConmentCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self creatUI];
    }
    return  self;
}
-(void)creatUI{
    
    self.iconImageView=[[UIImageView alloc]initWithFrame:CGRectMake(30*PERCEN, 20*PERCEN, 80*PERCEN, 80*PERCEN)];
    self.iconImageView.image=[UIImage imageNamed:@"touxiang_3.0"];
    self.iconImageView.layer.cornerRadius=self.iconImageView.height/2;
    self.iconImageView.layer.masksToBounds=YES;
    [self.contentView addSubview:self.iconImageView];
    
    self.nameLb=[[UILabel alloc]initWithFrame:CGRectMake(self.iconImageView.right+20*PERCEN, self.iconImageView.top, 200*PERCEN, 30*PERCEN)];
    self.nameLb.textColor=UIColorFromHex(0xffffff);
    self.nameLb.font=[UIFont systemFontOfSize:30*PERCEN];
 //   self.nameLb.text=@"Emily的妈妈";
    [self.contentView addSubview:self.nameLb];
    
    self.conmmentStyle=[[UILabel alloc]initWithFrame:CGRectMake(self.nameLb.right+20*PERCEN, self.nameLb.top, 100*PERCEN, 28*PERCEN)];
    self.conmmentStyle.textColor=UIColorFromHex(0xffffff);
    self.conmmentStyle.font=[UIFont systemFontOfSize:28*PERCEN];
    self.conmmentStyle.text=@"评论";
    [self.contentView addSubview:self.conmmentStyle];
    
    self.timeLb=[[UILabel alloc]initWithFrame:CGRectMake(self.nameLb.left, self.nameLb.bottom+20*PERCEN, 100, 24*PERCEN)];
    self.timeLb.textColor=UIColorFromHex(0xffffff);
    self.timeLb.font=[UIFont systemFontOfSize:24*PERCEN];
 //   self.timeLb.text=@"54分钟之前";
    [self.contentView addSubview:self.timeLb];


   
    
    self.replyBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    self.replyBtn.frame=CGRectMake(708*PERCEN, self.timeLb.top-20*PERCEN, 120*PERCEN, 30*PERCEN);

    [self.replyBtn setImage:[UIImage imageNamed:@"huifu_3.0"] forState:0];
    [self.replyBtn setTitle:@"回复" forState:0];
    self.replyBtn.titleLabel.font=[UIFont systemFontOfSize:30*PERCEN];
    [self.replyBtn setTitleColor:UIColorFromHex(0xffffff) forState:0];
    self.replyBtn.imageEdgeInsets=UIEdgeInsetsMake(0, -5*PERCEN, 0, 0);
    self.replyBtn.titleEdgeInsets=UIEdgeInsetsMake(0, 10*PERCEN, 0, 0);
    self.replyBtn.backgroundColor=[UIColor clearColor];
    [self.replyBtn addTarget:self action:@selector(onSelectReplyAction) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.replyBtn];
    
    self.commentLb=[[UILabel alloc]initWithFrame:CGRectMake(self.iconImageView.left, self.iconImageView.bottom, 838*PERCEN, 28*PERCEN)];
    self.commentLb.textColor=UIColorFromHex(0xffffff);
    self.commentLb.font=[UIFont systemFontOfSize:24*PERCEN];
    //   self.commentLb.text=@"录得太好了,QAQ";
    [self.contentView addSubview:self.commentLb];
    
//    self.replyLb=[[UILabel alloc]initWithFrame:CGRectMake(self.commentLb.left, self.commentLb.bottom, 838*PERCEN, 28*PERCEN)];
//    self.replyLb.textColor=UIColorFromHex(0xffffff);
//    self.replyLb.font=[UIFont systemFontOfSize:24*PERCEN];
//    //   self.commentLb.text=@"录得太好了,QAQ";
//    [self.contentView addSubview:self.replyLb];
    
    self.line=[[UIView alloc]initWithFrame:CGRectZero];
    self.line.backgroundColor=UIColorFromHex(0xffffff);
    [self.contentView addSubview:self.line];

}

-(void)setDateDic:(NSDictionary *)dateDic{
    if (!dateDic) {
        self.backgroundColor=[UIColor clearColor];
        [self removeAllSubviews];
    }
    _dateDic=dateDic;
    NSString *faceImg = [_dateDic objectForKey:@"FaceImg"];
    if (![faceImg isKindOfClass:[NSNull class]]) {
        [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",MUSIC_PLAY_Url,faceImg]] placeholderImage:[UIImage imageNamed:@"touxiang_3.0"]];
    } else {
      self.iconImageView.image = [UIImage imageNamed:@"touxiang_3.0"];
    }
    NSString*NickName=[_dateDic objectForKey:@"NickName"];
    
    float width =[NickName boundingRectWithSize:CGSizeMake(1000, 30*PERCEN) options:1|1 attributes:@{NSFontAttributeName:self.nameLb.font} context:nil].size.width;
    
    self.nameLb.text = [NSString stringWithFormat:@"%@",[_dateDic objectForKey:@"NickName"]];
    self.nameLb.frame=CGRectMake(self.iconImageView.right+20*PERCEN, self.iconImageView.top, width, 30*PERCEN);
   //
    self.conmmentStyle.frame= CGRectMake(self.nameLb.right+20*PERCEN, self.nameLb.top, 100*PERCEN, 28*PERCEN);
    _timeLb.text = [[NSString stringWithFormat:@"%@",[_dateDic objectForKey:@"CreateTime"]] getChaTime];
//Content
    _commentLb.text = [self replaceUnicode:[NSString stringWithFormat:@"回复:%@",[_dateDic objectForKey:@"Content"]]];
    
    _replyLb.hidden = _isHide;
    _replyLb.text=[self replaceUnicode:[NSString stringWithFormat:@"回复:%@",[_dateDic objectForKey:@"ReplyCont"]]];
    
    //_replyLb.text = [NSString stringWithFormat:@"回复:%@",[_dateDic objectForKey:@"ReplyCont"]];
    
    NSString *reply = [NSString stringWithFormat:@"%@",[UserManager standerDefult].userModel.UserID];
    if (_isShowUserNews) {
        //从我的消息盒子跳转，taUserId为评论人的ID
        _replyBtn.hidden = (_replyLb.isHidden == NO ? YES : NO);
    } else {
        //从播放器跳转
        if ([_taUserId intValue] == [reply intValue]) {
         
            //主播和当前帐号是一样的，回复按钮试回复内容判断
         _replyBtn.hidden = (_replyLb.isHidden == NO ? YES : NO);
        } else {
            //登录的帐号不是该主播，路人 则回复按钮隐藏
            _replyBtn.hidden = YES;
        }
    }
    

}
#pragma make---转码
- (NSString *)replaceUnicode:(NSString *)unicodeStr {
    //张三  \u5f20\u4e09
    
    NSString *tempStr1 = [unicodeStr stringByReplacingOccurrencesOfString:@"\\u" withString:@"\\U"];
    NSString *tempStr2 = [tempStr1 stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
    NSString *tempStr3 = [[@"\"" stringByAppendingString:tempStr2] stringByAppendingString:@"\""];
    NSData *tempData = [tempStr3 dataUsingEncoding:NSUTF8StringEncoding];
    NSString* returnStr = [NSPropertyListSerialization propertyListFromData:tempData
                                                           mutabilityOption:NSPropertyListImmutable
                                                                     format:NULL
                                                           errorDescription:NULL];
    

    return [returnStr stringByReplacingOccurrencesOfString:@"\\r\\n" withString:@"\n"];
}
//回复
- (void)onSelectReplyAction
{
    if (_delegate && [_delegate respondsToSelector:@selector(onSelectControlForReplyWithValue:withView:)]) {
        [_delegate onSelectControlForReplyWithValue:_dateDic withView:self];
    }
}
-(void)setIsHide:(BOOL)isHide{
    _isHide=isHide;
    if (_isHide) {
        self.replyLb.hidden=YES;
        self.line.frame=CGRectMake(0, self.commentLb.bottom+2*PERCEN, 858*PERCEN, 2*PERCEN);
      
      
    }else{
        self.line.frame=CGRectMake(0, self.replyLb.bottom+2*PERCEN, 858*PERCEN, 2*PERCEN);

    }
}
@end
